
#include "C:\SAFIRE\Y\Y_BIFn\Y_BIFn.y.h"

#include "C:\SAFIRE\Y\PH_SL.y.h"

#include "C:\SAFIRE\USERS\SIP_USER\ws\SIP\source\SIP_LIB\SIP_LIB.y.h"


typedef SIP_CDC_T [SIP_Signals_T SIP_G, PH_SL UDP_Lower_G, PH_SL UDP_Upper_G]
{
      
    SYNTAX                  SIP_Syntax;
    STRING                  PDU;
    PH_SL                   PH_SL_S;
    STRING                  ErrorDescription;
    BOOLEAN                 Result;
    SIP_SIP_Message_T       SIP_SIP_Message;   
    SIP_Signals_T           SIP_Signal;        
    STRING                  New_Line;
    Request_T               Request;
    Response_T              Response;
           
    Print("SIP_CODEC: --- started");
        
    Print("SIP_CODEC: --- loading the SIP_Syntax");  
    
        SIP_Syntax= Syntaxia_LoadSyntax("C:\\SAFIRE\\USERS\\SIP_USER\\ws\\SIP\\results\\SIP_CDC_TL\\SIP_CDC_TL.Syntaxia.vn");
        IF (SIP_Syntax EQ NULL)
        {
           Print("SIP_CODEC: --- ERROR when loading SIP_CDC_Syntaxia.vn");
           STOP;
        }
        ELSE
        {
           Print("SIP_CODEC: --- SIP_CDC_Syntaxia.vn was loaded successfully");                  
           :: Idle;
        }
    
    
    STATE Idle    
    {        
        UDP_Upper_G ? PH_SL.PH_Act_Rq()     : PH_SL_S  UDP_Lower_G ! PH_SL.PH_Act_Rq(); 
        UDP_Upper_G ? PH_SL.PH_Deact_Rq()   : PH_SL_S  UDP_Lower_G ! PH_SL.PH_Deact_Rq();        
        UDP_Lower_G ? PH_SL.PH_Deact_Ind()  : PH_SL_S  UDP_Upper_G ! PH_Deact_Ind();                 
        UDP_Lower_G ? PH_SL.PH_Act_Ind()    : PH_SL_S 
        {
            UDP_Upper_G ! PH_SL.PH_Act_Ind();
            :: Active;
        }            
    }
    
    
    STATE Active
    {        
        UDP_Upper_G ? PH_SL.PH_Act_Rq()     : PH_SL_S  UDP_Lower_G ! PH_SL.PH_Act_Rq();
        UDP_Upper_G ? PH_SL.PH_Deact_Rq()   : PH_SL_S  UDP_Lower_G ! PH_SL.PH_Deact_Rq();        
        UDP_Lower_G ? PH_SL.PH_Act_Ind()    : PH_SL_S  UDP_Upper_G ! PH_Act_Ind();
        UDP_Lower_G ? PH_SL.PH_Deact_Ind()  : PH_SL_S                  
        {
            UDP_Upper_G ! PH_Deact_Ind();
            :: Idle;            
        }       
        
        SIP_G? SIP_Signals_T.SIP_Invite(?)                                  :  SIP_Signal  Send_SIP_Request( SIP_Signal.SIP_Invite.Request, SIP_Syntax);        
        SIP_G? SIP_Signals_T.SIP_Register(?)    							:  SIP_Signal  Send_SIP_Request( SIP_Signal.SIP_Register.Request, SIP_Syntax);
        SIP_G? SIP_Signals_T.SIP_Ack(?)         							:  SIP_Signal  Send_SIP_Request( SIP_Signal.SIP_Ack.Request, SIP_Syntax);
        SIP_G? SIP_Signals_T.SIP_Bye(?)         							:  SIP_Signal  Send_SIP_Request( SIP_Signal.SIP_Bye.Request, SIP_Syntax);
        SIP_G? SIP_Signals_T.SIP_Cancel(?)                                  :  SIP_Signal  Send_SIP_Request( SIP_Signal.SIP_Cancel.Request, SIP_Syntax);
        SIP_G? SIP_Signals_T.SIP_Options (?)                                :  SIP_Signal  Send_SIP_Request( SIP_Signal.SIP_Options.Request, SIP_Syntax);
                
        SIP_G? SIP_Signals_T.SIP_100_Trying(?)                              :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_100_Trying.Response, SIP_Syntax);                   
        SIP_G? SIP_Signals_T.SIP_180_Ringing(?)                             :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_180_Ringing.Response, SIP_Syntax);                                  
        SIP_G? SIP_Signals_T.SIP_181_Call_Is_Being_Forwarded(?)             :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_181_Call_Is_Being_Forwarded.Response, SIP_Syntax);                                
        SIP_G? SIP_Signals_T.SIP_182_Queued(?)                              :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_182_Queued.Response, SIP_Syntax);                               
        SIP_G? SIP_Signals_T.SIP_183_Session_Progress(?)                    :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_183_Session_Progress.Response, SIP_Syntax);                                 
        SIP_G? SIP_Signals_T.SIP_200_OK(?)                                  :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_200_OK.Response, SIP_Syntax);                                
        SIP_G? SIP_Signals_T.SIP_202_Accepted(?)                            :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_202_Accepted.Response, SIP_Syntax);                             
        SIP_G? SIP_Signals_T.SIP_300_Multiple_Choices(?)                    :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_300_Multiple_Choices.Response, SIP_Syntax);                                      
        SIP_G? SIP_Signals_T.SIP_301_Moved_Permanently(?)                   :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_301_Moved_Permanently.Response, SIP_Syntax);                                      
        SIP_G? SIP_Signals_T.SIP_302_Moved_Temporarily(?)                   :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_302_Moved_Temporarily.Response, SIP_Syntax);                                       
        SIP_G? SIP_Signals_T.SIP_305_Use_Proxy(?)                           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_305_Use_Proxy.Response, SIP_Syntax);                                     
        SIP_G? SIP_Signals_T.SIP_380_Alternative_Service(?)                 :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_380_Alternative_Service.Response, SIP_Syntax);                                       
        SIP_G? SIP_Signals_T.SIP_400_Bad_Request(?)                         :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_400_Bad_Request.Response, SIP_Syntax);                                  
        SIP_G? SIP_Signals_T.SIP_401_Unauthorized(?)                        :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_401_Unauthorized.Response, SIP_Syntax);                                 
        SIP_G? SIP_Signals_T.SIP_402_Payment_Required(?)                    :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_402_Payment_Required.Response, SIP_Syntax);                                    
        SIP_G? SIP_Signals_T.SIP_403_Forbidden(?)                           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_403_Forbidden.Response, SIP_Syntax);                                    
        SIP_G? SIP_Signals_T.SIP_404_Not_Found(?)                           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_404_Not_Found.Response, SIP_Syntax);                                   
        SIP_G? SIP_Signals_T.SIP_405_Method_Not_Allowed(?)                  :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_405_Method_Not_Allowed.Response, SIP_Syntax);                                     
        SIP_G? SIP_Signals_T.SIP_406_Not_Acceptable(?)                      :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_406_Not_Acceptable.Response, SIP_Syntax);                                     
        SIP_G? SIP_Signals_T.SIP_407_Proxy_Authentication_Required(?)       :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_407_Proxy_Authentication_Required.Response, SIP_Syntax);    
        SIP_G? SIP_Signals_T.SIP_408_Request_Timeout(?)                     :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_408_Request_Timeout.Response, SIP_Syntax);    
        SIP_G? SIP_Signals_T.SIP_409_Conflict(?)                            :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_409_Conflict.Response, SIP_Syntax);                                                
        SIP_G? SIP_Signals_T.SIP_410_Gone(?)                                :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_410_Gone.Response, SIP_Syntax);    
        SIP_G? SIP_Signals_T.SIP_411_Length_Required(?)                     :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_411_Length_Required.Response, SIP_Syntax);     
        SIP_G? SIP_Signals_T.SIP_412_Conditional_Request_Failed(?)          :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_412_Conditional_Request_Failed.Response, SIP_Syntax);    
        SIP_G? SIP_Signals_T.SIP_413_Request_Entity_Too_Large(?)            :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_413_Request_Entity_Too_Large.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_414_Request_URI_Too_Large(?)               :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_414_Request_URI_Too_Large.Response, SIP_Syntax);                                                   
        SIP_G? SIP_Signals_T.SIP_415_Unsupported_Media_Type(?)              :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_415_Unsupported_Media_Type.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_416_Unsupported_URI_Scheme(?)              :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_416_Unsupported_URI_Scheme.Response, SIP_Syntax);                                               
        SIP_G? SIP_Signals_T.SIP_417_Unknown_Resource_Priority(?)           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_417_Unknown_Resource_Priority.Response, SIP_Syntax);    
        SIP_G? SIP_Signals_T.SIP_420_Bad_Extension(?)                       :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_420_Bad_Extension.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_421_Extension_Required(?)                  :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_421_Extension_Required.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_422_Session_Interval_Too_Small(?)          :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_422_Session_Interval_Too_Small.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_423_Interval_Too_Brief(?)                  :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_423_Interval_Too_Brief.Response, SIP_Syntax);                                               
        SIP_G? SIP_Signals_T.SIP_429_Provide_Referrer_Identity(?)           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_429_Provide_Referrer_Identity.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_480_Temporarily_Unavailable(?)             :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_480_Temporarily_Unavailable.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_481_Call_Leg_Transaction_Does_Not_Exist(?) :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_481_Call_Leg_Transaction_Does_Not_Exist.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_482_Loop_Detected(?)                       :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_482_Loop_Detected.Response, SIP_Syntax);                                                
        SIP_G? SIP_Signals_T.SIP_483_Too_Many_Hops(?)                       :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_483_Too_Many_Hops.Response, SIP_Syntax);
        SIP_G? SIP_Signals_T.SIP_484_Address_Incomplete(?)                  :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_484_Address_Incomplete.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_485_Ambiguous(?)                           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_485_Ambiguous.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_486_Busy_Here(?)                           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_486_Busy_Here.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_487_Request_Cancelled(?)                   :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_487_Request_Cancelled.Response, SIP_Syntax);                                                 
        SIP_G? SIP_Signals_T.SIP_488_Not_Acceptable_Here(?)                 :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_488_Not_Acceptable_Here.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_489_Bad_Event(?)                           :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_489_Bad_Event.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_491_Request_Pending(?)                     :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_491_Request_Pending.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_493_Undecipherable(?)                      :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_493_Undecipherable.Response, SIP_Syntax);                                               
        SIP_G? SIP_Signals_T.SIP_494_Security_Agreement_Required(?)         :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_494_Security_Agreement_Required.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_500_Server_Internal_Error(?)               :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_500_Server_Internal_Error.Response, SIP_Syntax);     
        SIP_G? SIP_Signals_T.SIP_501_Not_Implemented(?)                     :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_501_Not_Implemented.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_502_Bad_Gateway(?)                         :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_502_Bad_Gateway.Response, SIP_Syntax);   
        SIP_G? SIP_Signals_T.SIP_503_Service_Unavailable(?)                 :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_503_Service_Unavailable.Response, SIP_Syntax);       
        SIP_G? SIP_Signals_T.SIP_504_Gateway_Time_out(?)                    :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_504_Gateway_Time_out.Response, SIP_Syntax);       
        SIP_G? SIP_Signals_T.SIP_505_Version_Not_Supported(?)               :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_505_Version_Not_Supported.Response, SIP_Syntax);    
        SIP_G? SIP_Signals_T.SIP_513_Message_Too_Large(?)                   :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_513_Message_Too_Large.Response, SIP_Syntax);      
        SIP_G? SIP_Signals_T.SIP_600_Busy_Everywhere(?)                     :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_600_Busy_Everywhere.Response, SIP_Syntax);                                               
        SIP_G? SIP_Signals_T.SIP_603_Decline(?)                             :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_603_Decline.Response, SIP_Syntax);                                               
        SIP_G? SIP_Signals_T.SIP_604_Does_Not_Exist_Anywhere(?)             :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_604_Does_Not_Exist_Anywhere.Response, SIP_Syntax);        
        SIP_G? SIP_Signals_T.SIP_606_Not_Acceptable(?)                      :  SIP_Signal  Send_SIP_Response( SIP_Signal.SIP_606_Not_Acceptable.Response, SIP_Syntax);           
 
        UDP_Lower_G? PH_SL.PH_Data_Ind(?) : PH_SL_S
        {
            
            Print("SIP_CODEC: --- Received Data_Ind with:");
            Print( PH_SL_S.PH_Data_Ind.PDU);
           
            SIP_SIP_Message= Syntaxia_DecodeTextBuffer( SIP_Syntax,
                                                        PH_SL_S.PH_Data_Ind.PDU,                                                        
                                                        ErrorDescription);
            IF (SIP_SIP_Message EQ NULL)
            {
                Print("SIP_CODEC: --- Parsing PDU failed! ErrorDescription is:");
                Print(ErrorDescription);
				SIP_G ! SIP_Signals_T.SIP_Invalid( ErrorDescription);
            }
            ELSE
            {
                Print("SIP_CODEC: --- PDU decoded successfully:");                
                Print("\n\n" + $SIP_SIP_Message + "\n\n");                
                Print("SIP_CODEC: --- Analyzing received message");
                
                CHOICE(SIP_SIP_Message) 
                {
                    Request:    
                    {     
                        Request.Request_Line    = SIP_SIP_Message.Request.Request_Line;
                        Request.Message_Headers = Convert_Decode_Message_Headers(SIP_SIP_Message.Request.Message_Headers);
                        Request.Message_Body    = SIP_SIP_Message.Request.Message_Body;
                    
                        CHOICE (SIP_SIP_Message.Request.Request_Line.Method)
						{
							Invite:  	SIP_G ! SIP_Signals_T.SIP_Invite( Request);
							Ack:	 	SIP_G ! SIP_Signals_T.SIP_Ack( Request);
							Options:	SIP_G ! SIP_Signals_T.SIP_Options( Request);
							Bye:	 	SIP_G ! SIP_Signals_T.SIP_Bye( Request);
							Cancel:	 	SIP_G ! SIP_Signals_T.SIP_Cancel( Request);
							Register:	SIP_G ! SIP_Signals_T.SIP_Register( Request);
							
							OTHERWISE   Print( "SIP_CDC :  --- uknown Method");
						}
						                          
                    } 
                    
                    Response:
                    {
                        Response.Status_Line     = SIP_SIP_Message.Response.Status_Line;
                        Response.Message_Headers = Convert_Decode_Message_Headers(SIP_SIP_Message.Response.Message_Headers);
                        Response.Message_Body    = SIP_SIP_Message.Response.Message_Body;
                    
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 100)
							SIP_G ! SIP_Signals_T.SIP_100_Trying( Response);

                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 180)
							SIP_G ! SIP_Signals_T.SIP_180_Ringing( Response);							
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 180)
							SIP_G ! SIP_Signals_T.SIP_180_Ringing( Response);								
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 182)
							SIP_G ! SIP_Signals_T.SIP_182_Queued( Response);   
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 183)
							SIP_G ! SIP_Signals_T.SIP_183_Session_Progress( Response);        
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 200)
							SIP_G ! SIP_Signals_T.SIP_200_OK( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 202)
                            SIP_G ! SIP_Signals_T.SIP_202_Accepted( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 300)
                            SIP_G ! SIP_Signals_T.SIP_300_Multiple_Choices( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 301)
                            SIP_G ! SIP_Signals_T.SIP_301_Moved_Permanently( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 302)
                            SIP_G ! SIP_Signals_T.SIP_302_Moved_Temporarily( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 305)
                            SIP_G ! SIP_Signals_T.SIP_305_Use_Proxy( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 380)
                            SIP_G ! SIP_Signals_T.SIP_380_Alternative_Service( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 400)
                            SIP_G ! SIP_Signals_T.SIP_400_Bad_Request( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 401)
                            SIP_G ! SIP_Signals_T.SIP_401_Unauthorized( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 402)
                            SIP_G ! SIP_Signals_T.SIP_402_Payment_Required( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 403)
                            SIP_G ! SIP_Signals_T.SIP_403_Forbidden( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 404)
                            SIP_G ! SIP_Signals_T.SIP_404_Not_Found( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 405)
                            SIP_G ! SIP_Signals_T.SIP_405_Method_Not_Allowed( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 406)
                            SIP_G ! SIP_Signals_T.SIP_406_Not_Acceptable( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 407)
                            SIP_G ! SIP_Signals_T.SIP_407_Proxy_Authentication_Required( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 408)
                            SIP_G ! SIP_Signals_T.SIP_408_Request_Timeout( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 409)
                            SIP_G ! SIP_Signals_T.SIP_409_Conflict( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 410)
                            SIP_G ! SIP_Signals_T.SIP_410_Gone( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 411)
                            SIP_G ! SIP_Signals_T.SIP_411_Length_Required( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 412)
                            SIP_G ! SIP_Signals_T.SIP_412_Conditional_Request_Failed( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 413)
                            SIP_G ! SIP_Signals_T.SIP_413_Request_Entity_Too_Large( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 414)
                            SIP_G ! SIP_Signals_T.SIP_414_Request_URI_Too_Large( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 415)
                            SIP_G ! SIP_Signals_T.SIP_415_Unsupported_Media_Type( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 416)
                            SIP_G ! SIP_Signals_T.SIP_416_Unsupported_URI_Scheme( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 417)
                            SIP_G ! SIP_Signals_T.SIP_417_Unknown_Resource_Priority( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 420)
                            SIP_G ! SIP_Signals_T.SIP_420_Bad_Extension( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 421)
                            SIP_G ! SIP_Signals_T.SIP_421_Extension_Required( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 422)
                            SIP_G ! SIP_Signals_T.SIP_422_Session_Interval_Too_Small( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 423)
                            SIP_G ! SIP_Signals_T.SIP_423_Interval_Too_Brief( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 429)
                            SIP_G ! SIP_Signals_T.SIP_429_Provide_Referrer_Identity( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 480)
                            SIP_G ! SIP_Signals_T.SIP_480_Temporarily_Unavailable( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 481)
                            SIP_G ! SIP_Signals_T.SIP_481_Call_Leg_Transaction_Does_Not_Exist( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 482)
                            SIP_G ! SIP_Signals_T.SIP_482_Loop_Detected( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 483)
                            SIP_G ! SIP_Signals_T.SIP_483_Too_Many_Hops( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 484)
                            SIP_G ! SIP_Signals_T.SIP_484_Address_Incomplete( Response);   
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 485)
                            SIP_G ! SIP_Signals_T.SIP_485_Ambiguous( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 486)
                            SIP_G ! SIP_Signals_T.SIP_486_Busy_Here( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 487)
                            SIP_G ! SIP_Signals_T.SIP_487_Request_Cancelled( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 488)
                            SIP_G ! SIP_Signals_T.SIP_488_Not_Acceptable_Here( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 489)
                            SIP_G ! SIP_Signals_T.SIP_489_Bad_Event( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 491)
                            SIP_G ! SIP_Signals_T.SIP_491_Request_Pending( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 493)
                            SIP_G ! SIP_Signals_T.SIP_493_Undecipherable( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 494)
                            SIP_G ! SIP_Signals_T.SIP_494_Security_Agreement_Required( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 500)
                            SIP_G ! SIP_Signals_T.SIP_500_Server_Internal_Error( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 501)
                            SIP_G ! SIP_Signals_T.SIP_501_Not_Implemented( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 502)
                            SIP_G ! SIP_Signals_T.SIP_502_Bad_Gateway( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 503)
                            SIP_G ! SIP_Signals_T.SIP_503_Service_Unavailable( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 504)
                            SIP_G ! SIP_Signals_T.SIP_504_Gateway_Time_out( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 505)
                            SIP_G ! SIP_Signals_T.SIP_505_Version_Not_Supported( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 513)
                            SIP_G ! SIP_Signals_T.SIP_513_Message_Too_Large( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 600)
                            SIP_G ! SIP_Signals_T.SIP_600_Busy_Everywhere( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 603)
                            SIP_G ! SIP_Signals_T.SIP_603_Decline( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 604)
                            SIP_G ! SIP_Signals_T.SIP_604_Does_Not_Exist_Anywhere( Response);
                        IF (SIP_SIP_Message.Response.Status_Line.Status_Code EQ 606)
                            SIP_G ! SIP_Signals_T.SIP_606_Not_Acceptable( Response);                     
                     
                            
                     }                        
                     
                     OTHERWISE Print("it is not a Request or Response ... failure in decode function");  
                }     
            }                
                
        }
    }
};

VOID Send_SIP_Request( Request_T Request, SYNTAX SIP_Syntax)
{
    SIP_SIP_Message_T   SIP_SIP_Message;
    STRING              PDU;
    STRING              ErrorDescription;
    
    SIP_SIP_Message.Request.Request_Line = Request.Request_Line;
    SIP_SIP_Message.Request.Message_Headers = Convert_Encode_Message_Headers( Request.Message_Headers);
    SIP_SIP_Message.Request.Message_Body = Request.Message_Body;
               
    IF (Syntaxia_EncodeToTextBuffer( SIP_SIP_Message,
                                     SIP_Syntax,
                                     PDU,
                                     ErrorDescription))
        UDP_Lower_G! PH_SL.PH_Data_Rq(PDU);
	ELSE
		SIP_G ! SIP_Signals_T.SIP_Invalid( ErrorDescription);
                          
	RETURN;
}   

VOID Send_SIP_Response( Response_T Response, SYNTAX SIP_Syntax)
{
    SIP_SIP_Message_T SIP_SIP_Message;
    STRING              PDU;
    STRING              ErrorDescription;

    SIP_SIP_Message.Response.Status_Line = Response.Status_Line;    
    SIP_SIP_Message.Response.Message_Headers = Convert_Encode_Message_Headers( Response.Message_Headers);               
    SIP_SIP_Message.Response.Message_Body = Response.Message_Body;
               
    IF (Syntaxia_EncodeToTextBuffer( SIP_SIP_Message,
                                     SIP_Syntax,
                                     PDU,
                                     ErrorDescription))               
        UDP_Lower_G! PH_SL.PH_Data_Rq(PDU);
                          
    RETURN;         
}   

SIP_Message_Headers_T Convert_Encode_Message_Headers(Message_Headers_T Message_Headers)
{
    SIP_Message_Headers_T       SIP_Message_Headers;
    SIP_Message_Header_T        SIP_Message_Header;
    SIP_Message_Header_CRLF_T   SIP_Message_Header_CRLF;

    
    IF (Message_Headers EQ NULL) RETURN NULL;
    
    IF (Message_Headers.To NEQ NULL)
    {        
        SIP_Message_Header_CRLF.Message_Header.To = Message_Headers.To;
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
        
    IF (Message_Headers.From NEQ NULL)
    {        
        SIP_Message_Header_CRLF.Message_Header.From = Message_Headers.From;       
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
    
    IF (Message_Headers.Call_Id NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Call_Id = Message_Headers.Call_Id;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Contact NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Contact = Message_Headers.Contact;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.CSeq NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.CSeq = Message_Headers.CSeq;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Max_Forwards NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Max_Forwards = Message_Headers.Max_Forwards;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Via NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Via = Message_Headers.Via;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Accept NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Accept = Message_Headers.Accept;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
	
	IF (Message_Headers.Expires NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Expires = Message_Headers.Expires;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
	
	IF (Message_Headers.Date NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Date = Message_Headers.Date;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Allow NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Allow = Message_Headers.Allow;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Subject NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Subject = Message_Headers.Subject;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Content_Type NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Content_Type = Message_Headers.Content_Type;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }

    IF (Message_Headers.Content_Length NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Content_Length = Message_Headers.Content_Length;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
    
    IF (Message_Headers.Supported NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Supported = Message_Headers.Supported;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
    
    IF (Message_Headers.Unsuppored NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Unsuppored = Message_Headers.Unsuppored;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
    
    IF (Message_Headers.Allow_Events NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Allow_Events = Message_Headers.Allow_Events;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }
        
    IF (Message_Headers.User_Agent NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.User_Agent = Message_Headers.User_Agent;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    } 

    IF (Message_Headers.Server NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Server = Message_Headers.Server;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }      

    IF (Message_Headers.Call_Info NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Call_Info = Message_Headers.Call_Info;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }      

    IF (Message_Headers.P_Asserted_Identity NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.P_Asserted_Identity = Message_Headers.P_Asserted_Identity;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }  

    IF (Message_Headers.Event NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Event = Message_Headers.Event;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }  

    IF (Message_Headers.Refer_To NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Refer_To = Message_Headers.Refer_To;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }  
    
    IF (Message_Headers.Referred_By NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Referred_By = Message_Headers.Referred_By;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }  

    IF (Message_Headers.Subscription_State NEQ NULL)
    {              
        SIP_Message_Header_CRLF.Message_Header.Subscription_State = Message_Headers.Subscription_State;        
        Message_Header_Add(SIP_Message_Header_CRLF, SIP_Message_Headers);
    }      
    
    RETURN SIP_Message_Headers;
}

SIP_Message_Header_CRLF_T
Message_Header_Add( SIP_Message_Header_CRLF_T    Message_Header_CRLF,
                    SIP_Message_Headers_T       &Message_Headers_List)
{
    SIP_Message_Headers_T  *Message_Headers_List_Ptr;
    SIP_Message_Headers_T  *Message_Headers_List_Ptr_;    
    BOOLEAN                     Head;
    
    CHOICE( Message_Headers_List.Message_Header_CRLF.Message_Header)
    {
        Not_Valid: Head= TRUE;
        OTHERWISE Head= FALSE;
    }
    
    IF( Head EQ TRUE)
    {        
        Message_Headers_List.Message_Header_CRLF= Message_Header_CRLF;
        Message_Headers_List_Ptr_= &Message_Headers_List;
        
        RETURN( &Message_Headers_List_Ptr_->Message_Header_CRLF);
    }
    ELSE
    {        
        Message_Headers_List_Ptr= &Message_Headers_List;        
        
        WHILE( *Message_Headers_List_Ptr NEQ NULL)
        {
            Message_Headers_List_Ptr_= Message_Headers_List_Ptr;
            Message_Headers_List_Ptr= &Message_Headers_List_Ptr->Message_Headers;
        }        
        
        Message_Headers_List_Ptr_->Message_Headers.Message_Header_CRLF= Message_Header_CRLF;
        
        RETURN( &Message_Headers_List_Ptr_->Message_Headers.Message_Header_CRLF);
    }
}

Message_Headers_T Convert_Decode_Message_Headers(SIP_Message_Headers_T SIP_Message_Headers)
{
    Message_Headers_T       Message_Headers;
    SIP_Message_Headers_T   SIP_Message_Headers_;
    
    SIP_Message_Headers_ = SIP_Message_Headers;
    
    
    
    WHILE (SIP_Message_Headers_ NEQ NULL)
    {    
        
        CHOICE (SIP_Message_Headers_.Message_Header_CRLF.Message_Header)
        {
            Not_Valid:			Print("SIP_CDC - Not Valid Header found during decoding");
            Via:                Message_Headers.Via    				= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Via;
            From:               Message_Headers.From   				= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.From;
            To:                 Message_Headers.To     				= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.To;
            CSeq:               Message_Headers.CSeq   				= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.CSeq;
            Contact:            Message_Headers.Contact   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Contact;
            Subject:            Message_Headers.Subject   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Subject;
            Call_Id:            Message_Headers.Call_Id   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Call_Id;
            Content_Type:       Message_Headers.Content_Type   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Content_Type;
            Content_Length:     Message_Headers.Content_Length   	= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Content_Length;
            Max_Forwards:       Message_Headers.Max_Forwards   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Max_Forwards;
            Supported:          Message_Headers.Supported   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Supported;
            Unsupported:        Message_Headers.Unsupported   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Unsupported;
            Allow_Events:       Message_Headers.Allow_Events   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Allow_Events;
            Allow:              Message_Headers.Allow   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Allow;
            User_Agent:         Message_Headers.User_Agent   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.User_Agent;
            Server:             Message_Headers.Server   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Server;
            Call_Info:          Message_Headers.Call_Info   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Call_Info;
            P_Asserted_Identity:Message_Headers.P_Asserted_Identity	= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.P_Asserted_Identity; 
            Event:              Message_Headers.Event   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Event;
            Refer_To:           Message_Headers.Refer_To   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Refer_To;
            Referred_By:        Message_Headers.Referred_By   		= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Referred_By;
            Subscription_State: Message_Headers.Subscription_State	= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Subscription_State;
            Accept:             Message_Headers.Accept   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Accept;           
            Expires:            Message_Headers.Expires   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Expires;           
            Date:            	Message_Headers.Date   			= SIP_Message_Headers_.Message_Header_CRLF.Message_Header.Date;           
            
           OTHERWISE Print("Unknown SIP_CDC_Message_Headers.Message_Header");
        }        
        SIP_Message_Headers_ = SIP_Message_Headers_.Message_Headers;    
    }
    
    RETURN Message_Headers;
}
